package servlet;

import gameserver.SocketServer;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

import modle.Player;
import modle.Table;

public class getGamePlayersServelt extends HttpServlet {

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("application/json;charset=utf-8");
		response.setHeader("Pragma","No-cache");   
		  response.setHeader("Cache-Control","no-cache");   
		  response.setDateHeader("Expires", 0);  
		PrintWriter out = response.getWriter();
		
		int roomIndex = Integer.parseInt(request.getParameter("selectedRoom"));
		int tableIndex = Integer.parseInt(request.getParameter("currentTable"));
		int pos = Integer.parseInt(request.getParameter("pos"));
		
		
		
		SocketServer server = StartSocketServlet.socketServerList.get(roomIndex);
		
		Table table = server.tableMap.get(tableIndex);
//		String playersInfo = "";
		
		Player[] players;
		if(table != null){
			List<Player> list = table.players;
			players = new Player[list.size()];
			for(int i=0; i<players.length; i++){
				if(list.get(i) != null){
					players[i] = new Player();
					players[i].setName(list.get(i).getName());
					players[i].state = (list.get(i).state);
				}
			}
			out.println(new Gson().toJson(players));
			System.out.println("getGamePlayersServelt: " + new Gson().toJson(players));
		}
		
		
		  
		out.flush();
		out.close();
	}

}
